package com.jsh.erp.service.sequence;

import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.constants.BusinessConstants;
import com.jsh.erp.dao.mappers.SequenceMapperEx;
import com.jsh.erp.domain.entities.SerialNumber;
import com.jsh.erp.domain.entities.SerialNumberEx;
import com.jsh.erp.domain.entities.SystemConfig;
import com.jsh.erp.service.systemConfig.SystemConfigService;
import com.jsh.erp.utils.ExceptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

/**
 * Description
 *
 * @Author: jishenghua
 * @Date: 2021/3/16 16:33
 */
@Service
public class SequenceService {
    private Logger logger = LoggerFactory.getLogger(SequenceService.class);

    @Resource
    private SequenceMapperEx sequenceMapperEx;
    @Resource
    private SystemConfigService configService;


    public SerialNumber getSequence(long id) throws Exception {
        return null;
    }

    public List<SerialNumberEx> select(String name, Integer offset, Integer rows) throws Exception {
        return null;
    }

    public Long countSequence(String name) throws Exception {
        return null;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int insertSequence(JSONObject obj, HttpServletRequest request) throws Exception {
        return 0;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int updateSequence(JSONObject obj, HttpServletRequest request) throws Exception {
        return 0;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int deleteSequence(Long id, HttpServletRequest request) throws Exception {
        return 0;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int batchDeleteSequence(String ids, HttpServletRequest request) throws Exception {
        return 0;
    }

    public int checkIsNameExist(Long id, String serialNumber) throws Exception {
        return 0;
    }

    /**
     * 创建一个唯一的序列号
     * */
    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public String buildOnlyNumber() throws Exception {
        Long buildOnlyNumber = null;
        synchronized (this) {
            try {
                sequenceMapperEx.updateBuildOnlyNumber(BusinessConstants.DEPOT_NUMBER_SEQ); //编号+1
                buildOnlyNumber = sequenceMapperEx.getBuildOnlyNumber(BusinessConstants.DEPOT_NUMBER_SEQ);
            } catch (Exception e) {
                ExceptionUtil.writeFail(logger, e);
            }
        }
        // 获取当前年份
        int currentYear = LocalDate.now().getYear();
        // 获取年份的后两位
        int lastTwoDigits = currentYear % 100;
        if (buildOnlyNumber < BusinessConstants.SEQ_TO_STRING_MIN_LENGTH) {
            StringBuffer sb = new StringBuffer(buildOnlyNumber.toString());
            int len = BusinessConstants.SEQ_TO_STRING_MIN_LENGTH.toString().length() - sb.length();
            for (int i = 0; i < len; i++) {
                sb.insert(0, BusinessConstants.SEQ_TO_STRING_LESS_INSERT);
            }
            return lastTwoDigits + sb.toString();
        } else {
            return lastTwoDigits + buildOnlyNumber.toString();
        }
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public String buildSaleOutNumber() throws Exception {
        Long buildOnlyNumber = null;
        synchronized (this) {
            try {
                sequenceMapperEx.updateBuildOnlyNumber(BusinessConstants.SALEOUT_NUMBER_SEQ); //编号+1
                buildOnlyNumber = sequenceMapperEx.getBuildOnlyNumber(BusinessConstants.SALEOUT_NUMBER_SEQ);
            } catch (Exception e) {
                ExceptionUtil.writeFail(logger, e);
            }
        }
        List<SystemConfig> systemConfigList = configService.getSystemConfig();
        if (buildOnlyNumber < 9999L) {
            StringBuffer sb = new StringBuffer(buildOnlyNumber.toString());
            int len = 4 - sb.length();
            for (int i = 0; i < len; i++) {
                sb.insert(0, BusinessConstants.SEQ_TO_STRING_LESS_INSERT);
            }
            return "-" + systemConfigList.get(0).getFiscalYear() + "-" + sb.toString();
        } else {
            return "-" + systemConfigList.get(0).getFiscalYear() + "-" + buildOnlyNumber.toString();
        }
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public String buildChequeNumber() throws Exception {
        Long buildOnlyNumber = null;
        synchronized (this) {
            try {
                sequenceMapperEx.updateBuildOnlyNumber(BusinessConstants.CHEQUE_NUMBER_SEQ); //编号+1
                buildOnlyNumber = sequenceMapperEx.getBuildOnlyNumber(BusinessConstants.CHEQUE_NUMBER_SEQ);
            } catch (Exception e) {
                ExceptionUtil.writeFail(logger, e);
            }
        }
        List<SystemConfig> systemConfigList = configService.getSystemConfig();
        if (buildOnlyNumber < 9999L) {
            StringBuffer sb = new StringBuffer(buildOnlyNumber.toString());
            int len = 4 - sb.length();
            for (int i = 0; i < len; i++) {
                sb.insert(0, BusinessConstants.SEQ_TO_STRING_LESS_INSERT);
            }
            return "C-" + systemConfigList.get(0).getFiscalYear() + "-" + sb.toString();
        } else {
            return "C-" + systemConfigList.get(0).getFiscalYear() + "-" + buildOnlyNumber.toString();
        }
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public String buildMoneyInNumber() throws Exception {
        Long buildOnlyNumber = null;
        synchronized (this) {
            try {
                sequenceMapperEx.updateBuildOnlyNumber(BusinessConstants.MONEY_IN_NUMBER_SEQ); //编号+1
                buildOnlyNumber = sequenceMapperEx.getBuildOnlyNumber(BusinessConstants.MONEY_IN_NUMBER_SEQ);
            } catch (Exception e) {
                ExceptionUtil.writeFail(logger, e);
            }
        }
        List<SystemConfig> systemConfigList = configService.getSystemConfig();
        if (buildOnlyNumber < 9999L) {
            StringBuffer sb = new StringBuffer(buildOnlyNumber.toString());
            int len = 4 - sb.length();
            for (int i = 0; i < len; i++) {
                sb.insert(0, BusinessConstants.SEQ_TO_STRING_LESS_INSERT);
            }
            return "-" + systemConfigList.get(0).getFiscalYear() + "-" + sb.toString();
        } else {
            return "-" + systemConfigList.get(0).getFiscalYear() + "-" + buildOnlyNumber.toString();
        }
    }

    public static void main(String[] args) {
        // 获取当前年份
        int currentYear = LocalDate.now().getYear();
        System.out.println("当前年份: " + currentYear);

        // 获取年份的后两位
        int lastTwoDigits = currentYear % 100;
        System.out.println("年份的后两位是: " + lastTwoDigits);
        Long buildOnlyNumber = 10L;
        if (buildOnlyNumber < 9999L) {
            StringBuffer sb = new StringBuffer(buildOnlyNumber.toString());
            int len = 4 - sb.length();
            for (int i = 0; i < len; i++) {
                sb.insert(0, BusinessConstants.SEQ_TO_STRING_LESS_INSERT);
            }
            System.out.println("111: " + sb.toString());
        } else {
            System.out.println("222: " + buildOnlyNumber.toString());
        }
    }
}
